/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditarDados.java
 *
 * Created on 10/07/2009, 16:11:15
 */
package interfaceGraficaUsuario;

import controle.DadosUsuarioEnum;
import controle.Sistema;
import controle.UsuariosEnum;
import java.awt.Color;
import java.util.List;

/**
 *
 * @author João Paulo
 */
public class EditarDados extends javax.swing.JFrame {

    private List<String> usuario;
    private UsuariosEnum tipo;

    /** Creates new form EditarDados */
    public EditarDados() {
        initComponents();
    }

    /** Creates new form InterfaceUsuario com tipo de usuario*/
    public EditarDados(List<String> usuario, UsuariosEnum tipo) {
        this.usuario = usuario;
        this.tipo = tipo;
        initComponents();
        setTitle("Editar Dados");
        erro.setText("");
        sucesso.setText("");
        erro.setForeground(Color.RED);
        sucesso.setForeground(Color.BLUE);
        campoMatricula.setText(usuario.get(DadosUsuarioEnum.MATRICULA.ordinal()));
        campoNome.setText(usuario.get(DadosUsuarioEnum.NOME.ordinal()));
        campoEmail.setText(usuario.get(DadosUsuarioEnum.EMAIL.ordinal()));


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelMatricula = new javax.swing.JLabel();
        labelNome = new javax.swing.JLabel();
        labelEmail = new javax.swing.JLabel();
        campoMatricula = new javax.swing.JTextField();
        campoNome = new javax.swing.JTextField();
        campoEmail = new javax.swing.JTextField();
        botaoSalvar = new javax.swing.JButton();
        botaoLimpar = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        sucesso = new javax.swing.JLabel();
        erro = new javax.swing.JLabel();
        labelEdit = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(550, 350));

        labelMatricula.setText("Matricula:");

        labelNome.setText("Nome:");

        labelEmail.setText("E-Mail:");

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        botaoLimpar.setText("Limpar");
        botaoLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoLimparActionPerformed(evt);
            }
        });

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        sucesso.setFont(new java.awt.Font("Tahoma", 0, 12));

        labelEdit.setText("Preencha os campos abaixo para editar os seus dados");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(erro, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(sucesso, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(labelMatricula, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                                .addComponent(labelNome, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(labelEmail)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(botaoSalvar)
                                    .addGap(18, 18, 18)
                                    .addComponent(botaoLimpar)
                                    .addGap(18, 18, 18)
                                    .addComponent(botaoCancelar)
                                    .addGap(86, 86, 86))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(campoEmail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                                        .addComponent(campoNome, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                                        .addComponent(campoMatricula, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE))
                                    .addGap(21, 21, 21)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(labelEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(58, 58, 58))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(erro, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sucesso, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(labelEdit)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelMatricula))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNome))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelEmail))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoSalvar)
                    .addComponent(botaoLimpar)
                    .addComponent(botaoCancelar))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed

        if (editaUsuario(usuario)) {
            sucesso.setText("Dados atualizados com sucesso");
            erro.setText("");

        } else {
            erro.setText("Os dados não foram atualizados");
            sucesso.setText("");
        }
}//GEN-LAST:event_botaoSalvarActionPerformed

    private void botaoLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoLimparActionPerformed
        inicializaCamposUsuario();
}//GEN-LAST:event_botaoLimparActionPerformed

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        dispose();
}//GEN-LAST:event_botaoCancelarActionPerformed

    /**
     * cria um usuario (aluno ou moderador)
     * @param tipo - tipo de usuario
     * @throws IOException
     */
    public boolean editaUsuario(List<String> usuario) {
//        List<String> usuario = new ArrayList<String>();

        usuario.set(DadosUsuarioEnum.MATRICULA.ordinal(), campoMatricula.getText());
        usuario.set(DadosUsuarioEnum.NOME.ordinal(), campoNome.getText());
        usuario.set(DadosUsuarioEnum.EMAIL.ordinal(), campoEmail.getText());


        try {
            return Sistema.editaDadosUsuario(usuario, tipo);
        } catch (Exception e) {
            erro.setText(e.getMessage());
        }
        return false;
    }

    /**
     * inicializa o valor dos campos de adicionar usuario, ou seja,
     * reseta o valor para vazio
     */
    public void inicializaCamposUsuario() {

        campoMatricula.setText("");
        campoNome.setText("");
        campoEmail.setText("");

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EditarDados().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoLimpar;
    private javax.swing.JButton botaoSalvar;
    javax.swing.JTextField campoEmail;
    javax.swing.JTextField campoMatricula;
    javax.swing.JTextField campoNome;
    javax.swing.JLabel erro;
    private javax.swing.JLabel labelEdit;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelMatricula;
    private javax.swing.JLabel labelNome;
    javax.swing.JLabel sucesso;
    // End of variables declaration//GEN-END:variables
}
